//
//  MainTabBarController.swift
//  微博
//
//  Created by expai on 16/4/27.
//  Copyright © 2016年 guangJun.com.cn. All rights reserved.
//

import UIKit

class MainTabBarController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //ios7 before 字会变，图不变
        addChildViewControllers()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        //添加按钮
        setupComposeBtn()
    }
    
      func setupComposeBtn(){
        tabBar.addSubview(composeBtn)
        
        let width = UIScreen.mainScreen().bounds.width / CGFloat(viewControllers!.count)
        let rect = CGRect(x: 0, y: 0, width: width, height: 49)
        
        composeBtn.frame = CGRectOffset(rect, 2*width, 0)
        
    }
    func addChildViewControllers() {
        //1、获取json文件的路径
        let path = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)
        
        //2、通过json文件路径创建NSdata
        if let jsonPath = path
        {
            let jsonData = NSData(contentsOfFile: jsonPath)
            do{
                let dictArr = try NSJSONSerialization.JSONObjectWithData(jsonData!, options: NSJSONReadingOptions.MutableContainers)
                print(dictArr)
                
                for dict in dictArr as! [[String:String]]
                {
                    addChildViewController(dict["vcName"]!, title: dict["title"]!, imagenamed: dict["imageName"]!)
                }
            }catch
            {
                print(error)
                addChildViewController("HomeTableViewController", title: "首页", imagenamed: "tabbar_home")
                //DisVover
                addChildViewController("DiscoverTableViewController", title: "发现", imagenamed: "tabbar_discover")
                //MainView
                addChildViewController("NullViewController", title: "主界面", imagenamed: "")
                
                //Message
                addChildViewController("MessagesTableViewController", title: "消息", imagenamed: "tabbar_message_center")
                //profile
                addChildViewController("ProfileTableViewController", title: "个人中心", imagenamed: "tabbar_profile")
            }
            
        }

    }
    
    private func addChildViewController(childController: String, title: String, imagenamed:String) {
        
        //动态获取命名空间
        let ns = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
        print(ns)
        let clas:AnyClass = NSClassFromString(ns + "." + childController)!

        let vcCls = clas as! UIViewController.Type
        let vc = vcCls.init()
        print(vc)

        vc.tabBarItem.image = UIImage(named: imagenamed)
        vc.tabBarItem.selectedImage = UIImage(named: imagenamed + "_highlighted")
        let nav = UINavigationController()
        nav.addChildViewController(vc)
        vc.title = title
        
        addChildViewController(nav)
    }
    
    //懒加载
    private lazy var composeBtn:UIButton = {
        let button = UIButton()
        
        // 2.设置图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        // 3.设置背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        // 4.监听加号按钮点击
        button.addTarget(self, action:#selector(composeBtnClick), forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
    func composeBtnClick(){
        
        print(#function)
    }
}
